package com.caiyi.financial.nirvana.sms.mapper;

import com.caiyi.financial.nirvana.sms.base.mapper.BaseMapper;
import com.caiyi.financial.nirvana.sms.entity.SmsLog;
import com.caiyi.financial.nirvana.sms.querybean.LogQueryBean;
import com.caiyi.financial.nirvana.sms.vo.SmsLogVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;


@Mapper
public interface SmsLogMapper extends BaseMapper<SmsLog> {

    @SelectProvider(type = LogSqlProvider.class, method = "selectLogList")
    List<SmsLogVo> selectLogList(LogQueryBean bean);

    class LogSqlProvider {
        public String selectLogList(LogQueryBean bean) {
            return new SQL() {
                {
                    SELECT("id,type,method,parameter,response,account,ip,create_time");
                    FROM("sms_log");
                    if (bean.getType() != null) {
                        WHERE("type=#{type}");
                    }
                    if (!StringUtils.isEmpty(bean.getMethod())) {
                        WHERE("method=#{method}");
                    }
                    if(!StringUtils.isEmpty(bean.getParameter())){
                        WHERE("parameter LIKE concat('%',#{parameter},'%')");
                    }
                    if(!StringUtils.isEmpty(bean.getResponse())){
                        WHERE("response LIKE concat('%',#{response},'%')");
                    }
                    if(!StringUtils.isEmpty(bean.getAccount())){
                        WHERE("account=#{account}");
                    }
                    if(!StringUtils.isEmpty(bean.getIp())){
                        WHERE("ip=#{ip}");
                    }
                    if (bean.getStartTime() != null) {
                        WHERE("create_time >= #{startTime}");
                    }
                    if (bean.getEndTime() != null) {
                        WHERE("create_time <= date_add(#{endTime}, interval 1 day)");
                    }
                    ORDER_BY("id desc");

                }
            }.toString();
        }
    }
}
